// pages/askToBuy/askToBuy.js 
const app = getApp();
import httpsUtils from '../../utils/https.js';
import commonUtils from '../../utils/common.js';
import myUtils from '../../utils/util.js';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId:'',
    user:null,
    askToBuyList: [],
    askToBuyLenght: 1,
    current:1,
    hasNextPage:true,

    column:2, //显示列数

    isClass:false //是否显示分类选择
  },
  /**
   * 切换显示行数
   */
  checkCol: function (e) {
    this.setData({
      column: e.currentTarget.dataset.column,
    })
  },
  /**
   * 回首页
   */
  toindex: function() {
    wx.switchTab({
      url: '../index/index',
    })
  },
  /*发布*/
  askToBuyIssue: function (e) {
    //获取用户信息
    commonUtils.getUserInfoByToken(wx.getStorageSync('utoken'), 2,function(){
      wx.navigateTo({
        url: '../askToBuyIssue2/askToBuyIssue2?inIndex=false',
      })
    });
  },


  /**
   * 商家求购列表
   */
  shopWantBuy: function (e) {
    wx.showToast({
      title: '加载中...',
      icon: 'loading'
    })
    setTimeout(function () {
      wx.hideToast()
    }, 5000)
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/tailGoods/selectList?isXcx=2&sort=2&current=' + that.data.current + "&userId=" + that.data.userId,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        var askToBuyList = that.data.askToBuyList.concat(json.data.list);
        for(let obj of askToBuyList){
          var timeStr = that.timeFormat(obj.gmtDatetime)
          
          if (timeStr.split(" ").length > 1){
            obj.time2 = timeStr + "发布";
            obj.time3 = timeStr.split(" ")[0] + "发布";
            obj.time4 = timeStr.split(" ")[0] + "发布";

          }else{
            obj.time2 = timeStr;
            obj.time3 = timeStr;
            obj.time4 = timeStr;
          }
        }
        that.setData({
          askToBuyList: askToBuyList,
          askToBuyLenght: json.data.total,
          current: json.data.pageNum,
          hasNextPage: json.data.hasNextPage,
        })
      }
      wx.hideToast();
    })
  },
  /**
   * 时间转换
   */
  timeFormat: function (time) {
    var that = this;
    var nowTime = Date.parse(new Date());
    var subTime = (nowTime - Date.parse(new Date(time))) / 1000;
    var timeStr = '';
    if (subTime < 60) {
      timeStr = '刚刚发布';
    } else if (parseInt(subTime / 60) < 60) {
      timeStr = parseInt((subTime / 60)) + '分钟前发布';
    } else if (parseInt(subTime / (60 * 60)) < 24) {
      timeStr = parseInt(subTime / (60 * 60)) + '小时前发布';
    } else if (parseInt(subTime / (60 * 60 * 24)) < 2) {
      timeStr = '昨天' + time.split(" ")[1] +"发布";
    } else if (parseInt(subTime / (60 * 60 * 24)) < 3) {
      timeStr = '前天' + time.split(" ")[1] + "发布";
    } else {
      timeStr = time;
    }
    return timeStr;
  },
  /**
   * 获取求购人员信息
   */
  getUserMsg: function (e) {
    var that = this;
    httpsUtils.getData({
      _url: app.globalData._url + '/user/selectOne?id=' + that.data.userId,
      data: {},
    }, function (json) {
      if (json.data && json.success) {
        that.setData({
          user: json.data
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    var that = this;
    if (!myUtils.isBlank(options.id)){
      that.setData({
        userId: options.id
      })
      //查询求购
      that.shopWantBuy();

      //求购人员信息
      that.getUserMsg();
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
   
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    if (that.data.hasNextPage){
      that.setData({
        current: that.data.current + 1,
      })
      /*商家求购列表*/
      that.shopWantBuy();
    }
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    var that = this;
    return {
      title: '这些尾货特价，谁要？点开联系我',
      path: 'pages/askToBuyTailShare/askToBuyTailShare?id=' + that.data.userId,
    }
  },
})